<template>
  <div class="app-main">
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper" style="position: relative">
        <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="onQuery">
          <a-form-model-item label="访谈分类">
            <a-input v-model="pageFilter.name" :allowClear=true placeholder="请输入" />
          </a-form-model-item>
          <div class="btnBox">
            <a-form-model-item :wrapper-col="{ offset: 0 }" style="margin-bottom: 0">
              <a-button type="primary" @click="onQuery"> 查询 </a-button>
              <a-button style="margin-left: 10px" @click="onResetForm"> 重置 </a-button>
            </a-form-model-item>
          </div>
        </a-form-model>
      </div>
    </a-card>
    <a-card :bordered="false">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <div>
          <a-button type="primary" @click="onAdd()" :disabled="quanxian.realname != '管理员'" >新增模板</a-button>
        </div>
      </div>
      <!-- 表格展示 -->
      <div class="table-page-content">
        <z-table ref="table" rowKey="id" :showAlert="false" :expandIconColumnIndex="1" :showSelection="false"
          :loading="loading" :columns="columns" :dataSource="pageData" @change="onTableChange">
          <!--操作-->
          <template slot="action" slot-scope="text, record">
            <a-button @click="onDetail(record)" type="link" style="padding: 0">查看</a-button>
            <a-divider type="vertical" />
            <a-button @click="onEdit(record)" type="link" style="padding: 0">编辑</a-button>
            <a-divider type="vertical" />
            <a-button @click="onDelete(record)" type="link" style="padding: 0" :disabled="quanxian.realname != '管理员'" >删除</a-button>
          </template>
        </z-table>
      </div>
      <ModuleAddModal ref="moduleAddModalId" :title=title @ok="onQuery" />
      <ModuleDetailModal ref="moduleDetailModalId" :title=title @ok="onQuery" />
    </a-card>
  </div>
</template>

<script>
import * as interviewApi from '@/api/sjsk/help/interview.js'
import ModuleAddModal from './modules/ModuleAddModal'
import ModuleDetailModal from './modules/ModuleDetailModal'
import {USER_INFO} from "@/store/mutation-types";
import Vue from 'vue'
// Vue.ls.get(USER_INFO)
export default {
  components: {
    ModuleAddModal, ModuleDetailModal
  },
  data() {
    return {
      quanxian:{},
      labelCol: { span: 2 },
      wrapperCol: { span: 5 },
      loading: false,
      visible: false,
      reloading: false,
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) => {
            return `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
          }
        },
        {
          title: '访谈分类',
          dataIndex: 'interviewClassification',
        },
        {
          title: '创建人',
          dataIndex: 'createName',
          width: '12%',
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: '30%',
        },
        {
          title: '更新人',
          dataIndex: 'updateName',
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime',
        },
        {
          title: '操作',
          width: 250,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageData: {},
      title: ""
    }
  },
  mounted() {
    // this.onQuery()
    this.quanxian = Vue.ls.get(USER_INFO);
  },
  methods: {
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.pageFilter = {
        pageNo: 1,
        pageSize: 10,
      }
      this.refreshTable()
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      interviewApi.templateList(this.pageFilter).then((res) => {
        if (res.success) {
          this.pageData = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.success(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warning(res.message)
        })
    },

    onAdd() {
      this.title = "新增模板"
      this.$refs.moduleAddModalId.show()
    },
    onDetail(record) {
      this.$refs.moduleDetailModalId.show(record.id)
    },
    onEdit(record) {
      this.title = "编辑模板"
      this.$refs.moduleAddModalId.show(record.id)
    },
    onDelete(record) {
      let that = this;
      this.$confirm({
        title: `你确定要删除此访谈分类吗？`,
        okText: "确认",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          interviewApi.deleteInterviewTemplate({ id: record.id }).then((res) => {
            that.$message.success(res.message)
            that.onQuery();
          });
        }
      });
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = true
      this.$nextTick(() => (this.reloading = false))
    },
  },
}
</script>
<style lang="less" scoped>
.btnBox {
  position: absolute;
  left: 30%;
  top: 0;
  z-index: 120;
}

/deep/.ant-card-body {
  padding: 15px 24px;
}

/deep/.ant-row {
  margin-bottom: 0
}

/deep/.ant-col-2 {
  width: 5.8%;
}
</style>
